package com.ptswitch.material.pages.contract;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.project.AppRuntimeException;
import org.hxzon.util.EMailUtil;
import org.hxzon.util.EMailUtil.EMail;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.contract.Contract;
import com.ptswitch.material.model.contract.ContractItem;
import com.ptswitch.material.model.security.Role;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.ContractItemService;
import com.ptswitch.material.service.ContractService;
import com.ptswitch.material.service.ErrorInputCacheService;
import com.ptswitch.material.util.ContractUtil;
import com.ptswitch.material.util.MaterialUtil;
import com.ptswitch.material.util.security.PermissionUtil;

public class ExamineItems extends PageBase {
    @InjectPage
    private ViewContract viewContract;
    @Inject
    private ContractService contractSer;
    @Inject
    private ContractItemService contractItemSer;
    @Inject
    private ErrorInputCacheService errorInputCacheSer;
    @Property
    private long contractId;
    private Contract contract;
    @Property
    private List<ContractItem> list;
    @Property
    private String examineRemark;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        this.contractId = id;
        contract = contractSer.findById(id);
        checkPermitted(PermissionUtil.ContractItemExamine);
        list = errorInputCacheSer.getExamineItems(id, getVisitor());
        if (list == null) {
            list = contractItemSer.findByExamine(id, false);
        }
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return contractId;
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "examine")
    Object update() {
        checkPermitted(PermissionUtil.ContractItemExamine);
        //just check
        boolean error = false;
        for (ContractItem item : list) {
            if (item.getNumCurrentPass() > item.getNumUnExamine()) {
                error = true;
                item.setErrorMsg("本次通过数大于未验数");
//				throw AppRuntimeException.error(item.getCode() + "通过数大于已收货数");
            } else if (item.getNumCurrentUnPass() > item.getNumUnExamine()) {
                error = true;
                item.setErrorMsg("本次未通过数大于未验数");
            } else if (item.getNumCurrentPass() + item.getNumCurrentUnPass() > item.getNumUnExamine()) {
                error = true;
                item.setErrorMsg("本次通过数加上未通过数大于未验数");
//				throw AppRuntimeException.error(item.getCode() + "通过数加上未通过数大于已收货数");
            } else {
                item.setErrorMsg(null);//reset
            }
        }
        if (error) {
            errorInputCacheSer.cacheExamineItems(contractId, getVisitor(), list);
            throw AppRuntimeException.error("本次验货有误");
        }
        //update
        for (ContractItem item : list) {
            item.setNumPass(item.getNumCurrentPass() + item.getNumPass());
            item.setNumUnPass(item.getNumCurrentUnPass() + item.getNumUnPass());
            item.setExamineRemark(examineRemark);
        }
        contractItemSer.saveAll(list);
        errorInputCacheSer.removeExamineItems(contractId, getVisitor());
        // update contract examine ok
        contract = contractSer.findById(contractId, ContractService.Init_Items);
        contract.updateForItem();
        contractSer.save(contract);
        //send email
        EMail email = MaterialUtil.newEMail();
        email.addTo(contract.getCreater().getEmail());
        email.setSubject(contract.getProduction().getRealname() + "已验货");
        email.addHtml(MaterialUtil.linkForViewContract(getWebUrl(), contract));
        email.addHtml(MaterialUtil.buildMessageForExamineContractItem(list));
        EMailUtil.send(email);
        return viewContract.withInit(contractId);
    }

    @Override
    protected List<Role> appendRoleToVisitor(Uzer visitor) {
        return ContractUtil.appendThisRole(contract, visitor);
    }
}
